<?php
require_once ('config.php');
require_once ('databasefunctions.php');
require_once ('displayfunctions.php');
validate_user();
html_header();

//Show menu

switch ($_GET['action']) {
	case "add_form" :
		title_bar($langjobmaster);
		?>
		<br><? echo $langoperation . " - " . $langadd ?><br>
		<form method = "GET" action="jobmaster.php">
		<table>
			<tr bgcolor='<? echo $color1 ?>'>
				<td><? echo $langjobid ?>
				<td><? echo $langjobname ?>
				<td><? echo $langenablestatus ?>
			<tr>
				<td><input type = 'text' name = 'JobID' maxlength = '10'>
				<td><input type = 'text' name = 'JobName' maxlength = '50'>
				<td><select name = 'Enable'>
						<option value='0'><? echo $langdisable ?>
						<option value='1'><? echo $langenable ?>
					</select>
					<input type = 'hidden' name = 'action' value = 'add'>
				<td><input type = 'submit' value = '<? echo $langadd ?>'>
		</table>		
		</form>
		<?
		break;
		
	case "add" :
		if (empty($_GET['JobID'])) {
			message_box($langjobid . " " . $langcannotempty);
			?>
				<script>history.go(-1);</script>
			<?
			exit;						
		}
		$sql = "INSERT INTO jobmaster (JobID, JobName, Enable) values (";
		$sql .= "'" . strtoupper($_GET['JobID']) . "' ,";
		$sql .= "'" . $_GET['JobName'] . "', ";
		$sql .= "'" . $_GET['Enable'] . "'";
		$sql .= ")";
		
		$sql_result = sql_execute($sql);
		echo "<br>$langjobid $langadded<br>";
		title_bar($langjobmaster, 1);
		break;
		
	case "modify_form" :
		title_bar($langjobmaster, 1);
		// Obtain values from table
		$sql = "SELECT * FROM jobmaster WHERE JobID='" . $_GET['JobID']. "'";
		$sql_result = sql_execute($sql);
		$sql_data = mysql_fetch_array($sql_result);
		
		?>
		<br><? echo $langoperation . " - " . $langmodify ?><br>
		<form method = "GET" action="jobmaster.php">
		<table>
			<tr bgcolor='<? echo $color1 ?>'>
				<td><? echo $langjobid ?>
				<td><? echo $langjobname ?>
				<td><? echo $langenablestatus ?>
			<tr>
				<td><input type = 'text' name = 'JobID' value = '<? echo $_GET['JobID'] ?>' 
					maxlength = '10' readonly>
				<td><input type = 'text' name = 'JobName' maxlength = '50' value = '<?
					 echo $sql_data['JobName'] ?> '>
				<td><select name = 'Enable'>
					<?
					if ($sql_data['Enable'] == '0') {
						echo "<option value='0' SELECTED>$langdisable";
						echo "<option value='1'>$langenable";
					}
					else {
						echo "<option value='0'>$langdisable";
						echo "<option value='1' SELECTED>$langenable";
					}
					?>
					</select>
					<input type = 'hidden' name = 'action' value = 'modify'>
				<td><input type = 'submit' value = '<? echo $langmodify ?>'>
		</table>		
		</form>
		<?	
		break;
		
	case "modify" :
		$sql = "UPDATE jobmaster SET ";
		$sql .= "JobName = '" . $_GET['JobName'] . "', ";
		$sql .= "Enable = '" . $_GET['Enable'] . "' ";
		$sql .= "WHERE JobID = '" . $_GET['JobID'] . "'";
		
		$sql_result = sql_execute($sql);
		echo "<br>$langjobid $langmodified<br>";
		title_bar($langjobmaster, 1);
		break;
		
	case "delete" :
		$sql = "SELECT JobID FROM billtransaction WHERE JobID = '" . $_GET['JobID'] ."' LIMIT 1";
		$sql_result = sql_execute($sql);
		$sql_data = mysql_fetch_array($sql_result);
		if ($sql_data[0]) {
			$message = $langtransactionexist . "<br>" . $langcannotdeljobid . "<br>";
			message_box($message);
			?>
				<script>history.go(-1);</script>
			<?
			exit;
		}
		
		$sql = "DELETE FROM jobmaster WHERE JobID = '" . $_GET['JobID'] . "'";
		
		$sql_result = sql_execute($sql);
		echo "<br>$langjobid $langdeleted<br>";
		title_bar($langjobmaster, 1);
		break;

	case "count" :
		save_search_list();
		$url_text = retrieve_search_list();
		$sql = select_statement('jobmaster');		
		$sql_result = sql_execute($sql);
		$sql_data = mysql_fetch_array($sql_result);
		$no_of_record = $sql_data[0];
		
		?>
		<script>
		this.location = 'jobmaster.php?action=browse&total_record=<? echo $no_of_record ?>
						&record_offset=0<? echo $url_text ?>'
		</script>
		<?
		break;
					
	case "browse" :
		navigation_bar($langjobmaster,1);
		column_headings(array($langjobid, $langjobname, $langenablestatus));
		// List Jobs here
		$sql = select_statement('jobmaster',1);
		$sql.= " ORDER BY JobID LIMIT " . $_GET['record_offset'] . ", " . $_SESSION['RecordPerPage'];
		$sql_result = sql_execute($sql);
		
		while ($sql_data = mysql_fetch_array($sql_result)) {
			$rowcolor = row_color_change();
			echo "\n<tr bgcolor='$rowcolor'>";
			echo "<td>";
			echo "<a href = 'jobmaster.php?action=modify_form&JobID=".$sql_data['JobID'];
			echo "'>".$sql_data['JobID']."</a>";
			echo "<td>";
			echo $sql_data['JobName'];
			echo "<td>";
			if ($sql_data['Enable'] == 1) echo $langyes;
			else echo $langno;			
			echo "<td>";
			echo "<a href = 'jobmaster.php?action=delete&JobID=" . 
					$sql_data['JobID'] ."'>$langdelete</a>";
		}
		echo "</table>";
		break;
		
	default :
		title_bar($langjobmaster, 1);
		$field[] = array('title'=>$langjobid, 'field'=>'JobID', 'length'=>10);
		search_form($field, $_SERVER['PHP_SELF']);
		break;
}

html_footer();
?>